package com.lyj.service.impl;

import com.lyj.mapper.OldManMapper;
import com.lyj.pojo.Nurse;
import com.lyj.pojo.OldMan;
import com.lyj.pojo.SatisNurseName;
import com.lyj.pojo.Satisfaction;
import com.lyj.service.OldManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OldManServiceImpl implements OldManService{

    @Autowired
    OldManMapper oldManMapper;

    
    @Override
    public List<OldMan> queryAllOldMan() {
        List<OldMan> oldManList = oldManMapper.queryAllOldMan();

        return oldManList;
    }

    @Override
    public List<OldMan> queryOldManByName(String oldName) {
        List<OldMan> oldManList = oldManMapper.queryOldManByName(oldName);
        return oldManList;
    }

//添加老人，并添加进评价表
    @Override
    public Boolean addOldMan(OldMan oldMan) {
        Boolean flag = oldManMapper.addOldMan(oldMan);
        System.out.println("===old_id==="+oldMan.getOldId());
        int oldId = oldMan.getOldId();
        //在评价表中添加这个老人
        /*SatisfactionServiceImpl satisfactionService = new SatisfactionServiceImpl();
        Boolean aBoolean = satisfactionService.addSatisfaction(oldId);*/
        System.out.println("====addSatisfaction==="+oldId);
        List<Integer> arrayList = new ArrayList<Integer>();
        //查询管理老人人数的排名，取前五个人
        List<Integer> satisfactionList = oldManMapper.querySatisNurseId(oldId);
        if (satisfactionList.size()<5){
            satisfactionList.clear();
            satisfactionList.add(1);
            satisfactionList.add(2);
            satisfactionList.add(3);
            satisfactionList.add(4);
            satisfactionList.add(5);
        }

        System.out.println("===satisfactionList===="+satisfactionList);//===satisfactionList====[16, 15, 14, 12, 13, 2, 1]
        //遍历五个，分别加入nurse1--nurse5
        for (int i = 0; i < 5; i++) {
            int id = satisfactionList.get(i);
            arrayList.add(id);
        }
        System.out.println("==="+arrayList);
        //
        Boolean aBoolean = oldManMapper.addSatisfaction(oldId, arrayList.get(0), arrayList.get(1), arrayList.get(2), arrayList.get(3), arrayList.get(4));

        if (flag){
            aBoolean=true;
            return true;
        }
        return false;
    }

    @Override
    public Boolean delOldManById(int oldId) {
        Boolean aBoolean = oldManMapper.delOldManById(oldId);
        Boolean aBoolean1 = oldManMapper.delOldManStatis(oldId);
        if (aBoolean){
            if (aBoolean1){
                return true;
            }
        }
        return false;
    }
    //修改老人记录
    @Override
    public Boolean updateOldManById(OldMan oldMan) {
        Boolean aBoolean = oldManMapper.updateOldManById(oldMan);
        return aBoolean;
    }
//查询老人的备用护工
    @Override
    public SatisNurseName queryOldManStatis(int oldId) {

        SatisNurseName satisNurseName = new SatisNurseName();
        satisNurseName.setOldId(oldId);

        Satisfaction satisfaction = oldManMapper.queryStatisByOldId(oldId);
        satisNurseName.setSatisId(satisfaction.getSatisId());
       //查询satisfaction中每个护工的姓名
        Nurse nurse = oldManMapper.queryNurse(satisfaction.getNurse1());
        satisNurseName.setNurse1(satisfaction.getNurse1());
        satisNurseName.setNurseName1(nurse.getUsername());

        Nurse nurse2 = oldManMapper.queryNurse(satisfaction.getNurse2());
        satisNurseName.setNurse2(satisfaction.getNurse2());
        satisNurseName.setNurseName2(nurse2.getUsername());

        Nurse nurse3 = oldManMapper.queryNurse(satisfaction.getNurse3());
        satisNurseName.setNurse3(satisfaction.getNurse3());
        satisNurseName.setNurseName3(nurse3.getUsername());

        Nurse nurse4 = oldManMapper.queryNurse(satisfaction.getNurse4());
        satisNurseName.setNurse4(satisfaction.getNurse4());
        satisNurseName.setNurseName4(nurse4.getUsername());

        Nurse nurse5 = oldManMapper.queryNurse(satisfaction.getNurse5());
        satisNurseName.setNurse5(satisfaction.getNurse5());
        satisNurseName.setNurseName5(nurse5.getUsername());

        return satisNurseName;
    }

    @Override
    public Boolean updateBysatisId(Satisfaction satisfaction) {

        Boolean aBoolean = oldManMapper.updateBysatisId(satisfaction);
        return aBoolean;
    }


}
